package fr.uparis10.miage.m1p1.config;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
//import fr.uparis10.miage.m1p1.config.DepartementReader;
import fr.uparis10.miage.classic.m1.p3.Departement;
import fr.uparis10.miage.classic.m1.p3.ObjectJ;
import fr.uparis10.miage.m1p1.core.Employe;
import fr.uparis10.miage.m1p1.core.Projet;
import fr.uparis10.miage.m1p1.global.LogFactory;

/**
 * Singleton class
 * 
 * @author medved
 * 
 */
public class Configurator {
	private static Configurator theInstance = null;

	private Logger log = LogFactory.getInstance().getLogger(
			Configurator.class.getName());

	private ArrayList<Employe> mEmployeListe;
	private ArrayList<Projet> mProjetListe;
	private ArrayList<Departement> mDepartementListe;

	private HashMap<String, Departement> mDepartementMap;
	private HashMap<String, Projet> mProjetMap;
	private HashMap<Integer, Employe> mEmployeMap;
	
	private ObjectJ mObjectJ;

	/**
	 * Private constructor - singleton class
	 */
	private Configurator() {
	}
	
	private void initDepartementMap() {
		if (mDepartementListe == null) {
			log.log(Level.INFO, "The department list wasn't yet initialized!");
			return;
		}
		mDepartementMap = new HashMap<String, Departement>();
		for (Departement dept: mDepartementListe) {
			mDepartementMap.put(dept.getNom(), dept);
		}
	}
	
	private void initProjetMap() {
		if (mProjetListe == null) {
			log.log(Level.INFO, "The project list wasn't yet initialized!");
			return;
		}
		mProjetMap = new HashMap<String, Projet>();
		for (Projet proj: mProjetListe) {
			mProjetMap.put(proj.getThemeProjet(), proj);
		}
	}
	
	private void initEmployeMap() {
		if (mEmployeListe == null) {
			log.log(Level.INFO, "The employe list wasn't yet initialized!");
			return;
		}
		mEmployeMap = new HashMap<Integer, Employe>();
		for (Employe empl: mEmployeListe) {
			mEmployeMap.put(empl.getCodeEmploye(), empl);
		}
	}

	/**
	 * @return the mDepartementListe
	 */
	public ArrayList<Departement> getDepartementListe() {
		return mDepartementListe;
	}
	
	/**
	 * @return the mProjetListe
	 */
	public ArrayList<Projet> getProjetListe() {
		return mProjetListe;
	}

	/**
	 * @return the mEmployeListe
	 */
	public ArrayList<Employe> getEmployeListe() {
		return mEmployeListe;
	}
	
	/**
	 * @return the mDepartementMap
	 */
	public HashMap<String, Departement> getDepartementMap() {
		return mDepartementMap;
	}

	/**
	 * @return the mProjetMap
	 */
	public HashMap<String, Projet> getProjetMap() {
		return mProjetMap;
	}
	
	/**
	 * @return the mEmployeMap
	 */
	public HashMap<Integer, Employe> getEmployeMap() {
		return mEmployeMap;
	}
	
	/**
	 * @return the mObjectJ
	 */
	public ObjectJ getObjectJ() {
		return mObjectJ;
	}
	
	public static Configurator getInstance() {
		if (theInstance == null) {
			theInstance = new Configurator();
		}
		return theInstance;
	}

	public void init(File aFile) {
		Properties prop = new Properties();
		try {
			FileInputStream inStream = new FileInputStream(aFile);
			prop.load(inStream);
		} catch (FileNotFoundException exc) {
			log.log(Level.SEVERE, "An exception occured: ", exc);
			return;
		} catch (IOException exc) {
			log.log(Level.SEVERE, "An exception occured: ", exc);
			return;
		}
		File projetsFile, employeFile, objectjFile, deptFile;
		try {
			String propValue;
			propValue = prop.get("projet").toString();
			projetsFile = new File(propValue);

			propValue = prop.get("employe").toString();
			employeFile = new File(propValue);

			propValue = prop.get("objectj").toString();
			objectjFile = new File(propValue);

			propValue = prop.get("departement").toString();
			deptFile = new File(propValue);
		} catch (NullPointerException exc) {
			log.log(Level.SEVERE, "Ooops... Got some nulls.", exc);
			return;
		}
		if (!projetsFile.exists()) {
			log.log(Level.SEVERE, "Le fichier " + projetsFile.getName()
					+ " n'existe pas!");
			return;
		}
		if (!employeFile.exists()) {
			log.log(Level.SEVERE, "Le fichier " + employeFile.getName()
					+ " n'existe pas!");
			return;
		}
		if (!objectjFile.exists()) {
			log.log(Level.SEVERE, "Le fichier " + objectjFile.getName()
					+ " n'existe pas!");
			return;
		}
		if (!deptFile.exists()) {
			log.log(Level.SEVERE, "Le fichier " + deptFile.getName()
					+ " n'existe pas!");
			return;
		}
		mProjetListe = ProjetReader.getInstance().readProjet(projetsFile);
		initProjetMap();
		mDepartementListe = DepartementReader.getInstance().readDepartement(deptFile);
		initDepartementMap();
		mEmployeListe = EmployeReader.getInstance().readEmployes(employeFile);
		initEmployeMap();
		mObjectJ = ObjectJReader.getInstance().readObjectJ(aFile);
	}
}
